// index.js
// 获取应用实例
const app = getApp()
const jsutils = require("great-jsutils/index")

Page({
  data: {
    activeKey: 0,
    typeActive: 0,
    page:1,
    limit: 100,
    list: [],
    listnow: [],
    listtype: [],
    banner: '',
    datetime: '',
    cate: [],
    type: 0,
    shopCard: [],
    shopCardNum: 0,
    price: 0,
    tip: '',
    shopCardShow: false,
    shopCardList: [],
    peisong: {},
    jsutils
  },
  // 事件处理函数
  bindViewTap() {
    
  },
  onLoad() {
    this.getList()
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  onShow() {
    // setInterval(this.getDate, 1000);
  },
  getUserProfile(e) {
    
  },
  jumpUrl(e) {
    let detail = null
    for(let i in this.data.list) {
      if(e.currentTarget.dataset.id == this.data.list[i]['id']) {
        detail = this.data.list[i]
      }
    }
    wx.setStorage({
      key:"detail" + e.currentTarget.dataset.id,
      data:detail
    })
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + e.currentTarget.dataset.id,
    })
  },
  getList() {
    let list = []
    app.requestUrl('/index', {page: this.data.page, limit: this.data.limit, single: this.data.typeActive, type: this.data.type}, res=> {
      let list = res.all
      this.initList(res.cate[0]['id'], res.all)

      let banner = res.banner
      let cate = res.cate
      let peisong = res.peisong
      this.setData({
        list, banner, cate, peisong
      })
      wx.setStorage({
        key: 'pesiong',
        data: res.peisong
      })
    })
  },
  onChange(event) {
    let type = 0
    if(this.data.cate[event.detail]) {
      type = this.data.cate[event.detail]['id']
    }
    
    this.initList(type, this.data.list)
    // let listnow = this.data.listtype[type]
    // this.setData({
    //   activeKey: event.detail,
    //   page: 1,
    //   type,
    //   listnow
    // })
    // this.getList()
    // this.setData
  },
  selectType(e) {
    this.setData({
      typeActive: e.currentTarget.dataset.id,
      page:1
    })
    this.getList()
  },
  onChangeStepper(e){
    let item = jsutils.ArrayUtils.get(this.data.list, {id: e.currentTarget.dataset.id})
    if(!item)return
    let shopCard = this.data.shopCard
    let row = {
      id: item.id,
      num: e.detail,
      row: item
    }
    if(jsutils.ArrayUtils.get(shopCard, row)) {
      jsutils.ArrayUtils.update(shopCard, row)
    }else{
      shopCard.push(row)
    }
    console.log(shopCard)

    this.setData({
      shopCard
    })
    
    this.setNumberPriceText(shopCard)
    
  },
  onChangeShowStepper(e){
    let shopCard = this.data.shopCard
    let item = jsutils.ArrayUtils.get(this.data.list, {id: e.currentTarget.dataset.id})
    if(!item)return
    let row = {
      id: item.id,
      num: 1,
      row: item
    }
    if(jsutils.ArrayUtils.get(shopCard, row)) {
      jsutils.ArrayUtils.update(shopCard, row)
    }else{
      shopCard.push(row)
    }
    this.setData({
      shopCard
    })
    
    this.setNumberPriceText(shopCard)
  },
  setNum(id, num, list){
    if(!list)list = this.data.list
    for(var i in list){
      if(list[i]['id'] == id) {

      }
    }
  },
  setNumberPriceText(shopCard){
    let a = 0
    let price = 0
    let shopCardList = []
    for(let i in shopCard) {
      if(shopCard[i]['num']>=0) {
        if(shopCard[i]['num']>0)a++;
        price += shopCard[i]['row']['price'] * shopCard[i]['num'] * 100
        shopCardList.push(shopCard[i])
      }
    }
    
    this.setCardAddButton(shopCard)

    this.setData({
      shopCardNum: a,
      price,
      shopCardList
    })
    wx.setStorage({
      key:"shopCardList",
      data: shopCardList
    })
  },
  initCacheList(){
    wx.getStorage({
      key:"shopCardList",
      success:res => {
        if(!res.data || res.data.length <=0) {
          return
        }
        var shopCard = []
        var price = 0
        var a = 0;
        for(var i in res.data){
          // shopCard[res.data[i]['row']['id']] = res.data[i]
          price += res.data[i]['num'] * res.data[i]['row']['price'] * 100
          if(res.data[i]['num']>0)a++
          // console.log(price)
        }
        this.setCardAddButton(res.data)
        this.setData({
          shopCardList: res.data,
          shopCardNum: a,
          shopCard: res.data,
          price
        })
      }
    })
  },
  showShopCard(){
    this.setData({
      shopCardShow: !this.data.shopCardShow
    })
  },
  setCardAddButton(shopCard, listnow){
    if(!listnow) {
      listnow = this.data.listnow
    }
    let tmp
    for(var i in listnow){
      tmp = jsutils.ArrayUtils.get(shopCard, {id: listnow[i]['id']})

      listnow[i]['showadd'] = tmp?true:false
      listnow[i]['num'] = tmp?tmp['num']:0
    }
    this.setData({
      listnow
    })
  },
  initList(type, list){

    let listnow = []
    for(var i in list) {
      if(type == list[i]['type']) {
        listnow[i] = list[i]
      }
    }
    this.setData({
      listnow
    })

    this.initCacheList()
  },
  onSubmit(){
    if(this.data.shopCardNum <= 0) {
      wx.showToast({
        title: '请选择菜品',
        icon: 'none',
        duration: 2000
      })      
      return
    }
    wx.navigateTo({
      url: '/pages/shopCard/shopCard'
    })
  },
  getDate(){
    //获取当前时间
    var date = new Date();
    //格式化为本地时间格式
    let datetime = date.getFullYear() + '-' + (date.getMonth() + 1)+ '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':'+ date.getSeconds()
    // var datetime = date.toLocaleString('chinese', {hour12: false});
    //获取div
    this.setData({
      datetime
    })
  }
})
